package com.atguigu.tingshu.order.mapper;

import com.atguigu.tingshu.model.order.OrderInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface OrderInfoMapper extends BaseMapper<OrderInfo> {

    /**
     * 查询用户本次购买的专辑是否存在未支付或已支付的订单
     * @param userId
     * @param itemId
     * @return
     */
    @Select("SELECT\n" +
            "\t COUNT( 1 ) \n" +
            "FROM\n" +
            "\t order_info t1\n" +
            "\t INNER JOIN order_detail t2 ON t2.order_id = t1.id \n" +
            "WHERE\n" +
            "\t t1.user_id = #{userId} \n" +
            "\t AND t1.item_type = '1001' \n" +
            "\t AND t2.item_id = #{itemId} \n" +
            "\t AND ( t1.order_status = '0901' OR t1.order_status = '0902' ) \n" +
            "\t AND t1.is_deleted = 0 \n" +
            "\t AND t2.is_deleted = 0")
    int selectAlbumUnpaidOrderCount(@Param("userId") Long userId,@Param("itemId") Long itemId);

    /**
     * 查询用户下单但未付款的声音id列表
     * @param userId
     * @return
     */
    @Select("SELECT\n" +
            "\t t2.item_id\n" +
            "FROM\n" +
            "\t order_info t1\n" +
            "\t INNER JOIN order_detail t2 ON t2.order_id = t1.id \n" +
            "WHERE\n" +
            "\t t1.item_type = '1002' \n" +
            "\t AND t1.order_status = '0901' \n" +
            "\t AND t1.user_id = #{userId} \n" +
            "\t AND t1.is_deleted = 0 \n" +
            "\t AND t2.is_deleted = 0")
    List<Long> selectTrackIdList(@Param("userId") Long userId);

    /**
     * 分页查询订单列表
     * @param page
     * @param userId
     * @return
     */
    Page<OrderInfo> selectUserOrderPage(Page<OrderInfo> page, @Param("userId") Long userId);
}
